package com.global.model;

import java.io.Serializable;
import javax.persistence.*;

import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;

import com.global.comparator.GridColumnComparator;

import java.util.List;


/**
 * The persistent class for the ZLY_ST_BSC_GRID database table.
 * @author zly601436
 */
@Entity()
@org.hibernate.annotations.Entity(dynamicUpdate=true)
@Table(name="ZLY_ST_BSC_GRID")
@SequenceGenerator(name="ZLY_ST_BSC_GRID_ID_GENERATOR", sequenceName="ZLY_ST_BSC_GRID_ID_SEQ", initialValue=100, allocationSize=1)
@NamedQueries({
        @NamedQuery(
                name = "findGridByName",
                query = "select g from Grid g where upper(g.name) = :uniqueGridName "
        ),
        @NamedQuery(
                name = "findGridByReport",
                query = "select g from Grid g where g.report.id = :reportId "
        )      
})
public class Grid extends BaseObject implements Serializable {

	private static final long serialVersionUID = 6629661713141557397L;
	
	private Long id;
	private String name;
	private String description;
	private int displayIndex;
	
	private List<GridColumn> gridColumnList;
	private DataSourceX dataSourceX;
	private Report report;

    /**
     * 定义操作人、操作时间
     */
    private Who who;
	
    public Grid() {
    }

	@Id	
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ZLY_ST_BSC_GRID_ID_GENERATOR")
	@SearchableId
	public Long getId() {
		return this.id;
	}
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * 返回Grid的名称
	 * @return String
	 */
	@Column(nullable=false, length=128, unique=true)
	@SearchableProperty
	public String getName() {
		return this.name;
	}
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 返回Grid的描述
	 * @return String
	 */
	@Column(nullable=false, length=1024)
	@SearchableProperty
	public String getDescription() {
		return this.description;
	}
	/**
	 * 设置Grid的描述
	 * @param description
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	
	/**
	 * 返回Grid的显示顺序
	 * @return int
	 */
	@Column(name="DISPLAY_INDEX", nullable=false)
	public int getDisplayIndex() {
		return displayIndex;
	}
	/**
	 * 设置Grid的显示顺序
	 * @param displayIndex
	 */
	public void setDisplayIndex(int displayIndex) {
		this.displayIndex = displayIndex < 0 ? 0 : displayIndex;
	}
	
	/**
	 * 是否为显示的Grid
	 * @return
	 */
	@Transient
	public boolean isDisplay() {
		return displayIndex>0 ? true : false;
	}

	/**
	 * 获取Grid中的组件清单
	 * @return
	 */
	//bi-directional many-to-one association to GridColumn
	@OneToMany(mappedBy="grid")
	@Sort(type=SortType.COMPARATOR,comparator=GridColumnComparator.class)
	public List<GridColumn> getGridColumnList() {
		return this.gridColumnList;
	}
	/**
	 * 设置Grid中的组件清单
	 * @param gridColumnList
	 */
	public void setGridColumnList(List<GridColumn> gridColumnList) {
		this.gridColumnList = gridColumnList;
	}	
	
	//bi-directional many-to-one association to DataSourceX
	@ManyToOne()
	@JoinColumn(name="DSO_ID", nullable=false)
	public DataSourceX getDataSourceX() {
		return dataSourceX;
	}
	public void setDataSourceX(DataSourceX dataSourceX) {
		this.dataSourceX = dataSourceX;
	}

	/**
	 * 获取Grid所属的Report
	 * @return
	 */
	//bi-directional many-to-one association to report
    @ManyToOne
	@JoinColumn(name="RPTX_ID", nullable=false)
	public Report getReport() {
		return this.report;
	}
    /**
     * 设置Grid所属的Report
     * @param report
     */
	public void setReport(Report report) {
		this.report = report;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Who getWho() {
		return who;
	}
	/**
     * {@inheritDoc}
     */
	@Override
	public void setWho(Who who) {
		this.who = who;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj){
			return true;
		}
		if (obj == null){
			return false;
		}
		if (getClass() != obj.getClass()){
			return false;
		}
		
		Grid other = (Grid) obj;
		
		if (id != other.id){
			if (name == null) {
				if (other.name != null)
					return false;
			} else if (!name.equalsIgnoreCase(other.name)){
				return false;
			}
		}
		
		return true;
	}

	@Override
	public String toString() {
		return "Grid [name=" + name + ", description=" + description
				+ ", report=" + report.getDescription() + "]";
	}	
}